#coding:utf-8
import os,sys
import re
sys.path.append("matchutils")
from utils import pygtrie
from config import *

class EditionTool(object):
    """ 版本策略"""
    def __init__(self):
        self.edwords = pygtrie.CharTrie()
        f = open( dict_dir +'/edwords.utf8.txt', 'rb')
        for line in f:
            line = line.rstrip()
            if line =='': continue
            if line.startswith('#'):
                continue
            fields = line.split()
            word = fields[0].decode('utf-8', 'ignore')
            dst = word
            if len(fields) == 2:
                dst = fields[1].decode('utf-8', 'ignore')
            self.edwords[ word] = dst
        f.close()
        self.video_series = {}
        f = open( dict_dir +'/video_series.txt', 'rb')
        for line in f:
            line = line.strip()
            if line == '': continue
            line = line.lower()
            self.video_series[ line ] = 1
        f.close()

        self.stopwords = {}
        self.stopwords["tv"] = 1
        self.stopwords["tv版"] = 1
        self.stopwords["卫视版"] = 1
        self.stopwords["卫视"] = 1
        self.stopwords["国语"] = 1
        self.stopwords["国语版"] = 1
        self.stopwords["内地版"] = 1
        self.stopwords["内地"] = 1
        self.stopwords["4k"] = 1
        self.stopwords["4k版"] = 1
        self.stopwords["hd版"] = 1
        self.stopwords["hd"] = 1
        self.stopwords["大陆版"] = 1
        self.stopwords["大陆"] = 1		
        self.stopwords["译制版"] = 1
        self.stopwords["译制"] = 1
        self.stopwords["3d"] = 1
        self.stopwords["2d"] = 1
        self.stopwords["3d版"] = 1
        self.stopwords["3d"] = 1
        self.stopwords["d版"] = 1
        self.stopwords["杜比"] = 1
        self.stopwords["原声"] = 1
        self.stopwords["中国"] = 1
        self.stopwords["tvb"] = 1
        self.stopwords["tvb版"] = 1
        self.stopwords["央视"] = 1
        self.stopwords["央视版"] = 1
        self.stopwords["中国"] = 1
        self.stopwords["中国版"] = 1
        self.stopwords["免费"] = 1
        self.stopwords["免费版"] = 1
        self.stopwords["重映版"] = 1
        self.stopwords["重映"] = 1
        self.stopwords["标清"] = 1
        self.stopwords["标清版"] = 1
        self.stopwords["高清"] = 1
        self.stopwords["高清版"] = 1
        self.stopwords["vr版"] = 1
        self.stopwords["bd版"] = 1
        self.stopwords["bd"] = 1
        self.stopwords["vr"] = 1
        self.stopwords["日播"] = 1
        self.stopwords["日播版"] = 1
        self.stopwords["完整版"] = 1
        self.stopwords["完整"] = 1
        self.stopwords["个人版"] = 1
        self.stopwords["个人"] = 1
        #国版
        self.stopwords["台版"] = 1
        self.stopwords["港版"] = 1
        self.stopwords["日版"] = 1
        self.stopwords["德版"] = 1
        self.stopwords["法版"] = 1
        self.stopwords["西版"] = 1
        self.stopwords["葡版"] = 1
        self.stopwords["泰版"] = 1
        self.stopwords["俄版"] = 1
        self.stopwords["美版"] = 1
        self.stopwords["奥版"] = 1
        self.stopwords["英版"] = 1
        self.stopwords["加版"] = 1
        self.stopwords["澳版"] = 1
        self.stopwords["韩版"] = 1
        self.stopwords["马来版"] = 1
        self.stopwords["菲律宾版"] = 1
        self.stopwords["菲律宾"] = 1
        self.stopwords["美国版"] = 1
        self.stopwords["美国"] = 1
        self.stopwords["加拿大"] = 1
        self.stopwords["加拿大版"] = 1
        self.stopwords["澳大利亚"] = 1
        self.stopwords["澳大利亚版"] = 1
        self.stopwords["澳洲"] = 1
        self.stopwords["澳洲版"] = 1
        self.stopwords["奥地利"] = 1
        self.stopwords["奥地利版"] = 1
        self.stopwords["英国版"] = 1
        self.stopwords["英国"] = 1
        self.stopwords["越"] = 1
        self.stopwords["越南"] = 1
        self.stopwords["英"] = 1
        self.stopwords["日"] = 1
        self.stopwords["美"] = 1
        self.stopwords["港"] = 1
        self.stopwords["泰"] = 1
        self.stopwords["台"] = 1
        self.stopwords["韩"] = 1
        self.stopwords["印"] = 1
        self.stopwords["越"] = 1
        self.stopwords["俄"] = 1
        self.stopwords["韩国"] = 1
        self.stopwords["韩国版"] = 1
        self.stopwords["台湾"] = 1
        self.stopwords["香港"] = 1
        self.stopwords["香港版"] = 1
        self.stopwords["日本"] = 1
        self.stopwords["日本版"] = 1
        self.stopwords["俄罗斯"] = 1
        self.stopwords["俄国"] = 1
        self.stopwords["俄国版"] = 1
        self.stopwords["中国版"] = 1
        self.stopwords["印度版"] = 1
        self.stopwords["印度"] = 1
        self.stopwords["法国"] = 1
        self.stopwords["法国版"] = 1
        self.stopwords["法国版"] = 1
        self.stopwords["德国版"] = 1
        self.stopwords["德国"] = 1
        self.stopwords["意大利版"] = 1
        self.stopwords["意大利"] = 1
        self.stopwords["西班牙版"] = 1
        self.stopwords["西班牙"] = 1
        self.stopwords["越南版"] = 1
        self.stopwords["朝鲜版"] = 1
        self.stopwords["马来版"] = 1
        self.stopwords["新加坡版"] = 1
        self.stopwords["新加坡"] = 1
        self.stopwords["新版"] = 1
        self.stopwords["葡萄牙"] = 1
        self.stopwords["葡萄牙版"] = 1
        self.stopwords["泰国版"] = 1
        self.stopwords["泰国"] = 1
        self.stopwords["内地"] = 1
        self.stopwords["大陆版"] = 1
        self.stopwords["大陆"] = 1
        self.stopwords["内地版"] = 1
        self.stopwords["内地"] = 1


    def is_unicode_hanzi(self, c):
        d = ord(c)
        #基本汉字
        if d >= 0x4E00 and d <= 0x9FA5:
            return True
        #补充汉字
        if d >= 0x9FA6 and d <= 0x9FCB:
            return True
        #扩展A
        if d >= 0x3400 and d <= 0x4DB5:
            return True
        #兼容汉字
        if d >= 0xF900 and d <= 0xFAD9:
            return True
        return False

    def ext_edition(self, uname):
        uname = uname.lower()
        uname = re.sub(u'[-《》·_—:：〖〗【】（）\(\)\[\]]', u' ', uname)
        results = {}
        words = uname.split()
        for w in words:
            if w == u'卫视':
                results["卫视"] = 1
                continue
            if w == u'国':
                results["国语"] = 1
                continue
            elif w == u'粤':
                results["粤语"] = 1
                continue
            elif w == u'台':
                results["台"] = 1
                continue
            elif w == u'港':
                results["香港"] = 1
                continue
            elif w == u'英':
                results["英"] = 1
                continue
            elif w == u'法':
                results["法"] = 1
                continue
            elif w == u'德':
                results["德"] = 1
                continue
            elif w == u'日':
                results["日"] = 1
                continue
            elif w == u'韩':
                results["韩"] = 1
                continue
            elif w == u'dvd':
                results["dvd"] = 1
                continue
            elif w == u'ova':
                results["ova"] = 1
                continue
        uname = uname.lower()
        num = len(uname)
        i = 0
        while i < num:
            res, val = self.edwords.longest_prefix( uname[i:])
            if res is None:
               i += 1
               continue
            sz = len(res)
            word = res.encode('utf-8', 'ignore')
            if word.isalnum():
                #英文
                if i > 0 and uname[i-1].isalnum():
                    i += 1
                    continue
                if i + sz < num and uname[i+sz-1].isalpha():
                    i += 1
                    continue
            elif len(res) == 1:
                #一个汉字
                if i > 0 and self.is_unicode_hanzi( uname[i-1]):
                    i += 1
                    continue
                if i + sz < num and self.is_unicode_hanzi(uname[i+sz-1]):
                    i += 1
                    continue
            elif len(res) == 2 and res[-1] == u'国':
                if i > 0 and self.is_unicode_hanzi( uname[i-1]):
                    i += 1
                    continue
                if i + sz < num and self.is_unicode_hanzi(uname[i+sz-1]):
                    i += 1
                    continue
            i += sz
            word = val.encode('utf-8', 'ignore')
            if word == 'dvd版' or word == 'ova版':
                word = word[:-3]
            results[word] = 1
            continue
        return results.keys()

    def get_diff_words(self, xdict, ydict, vt ):
        words = []

        ywords = ' '.join( ydict.keys() )
        for w in xdict:
            ori = w
            if xdict[w] == 0: continue
            if w in ydict:
                continue
            if w.endswith('版') and len(w)-3 > 3:
                w = w[:-3]
            elif w.endswith('篇') and len(w)-3> 3:
                w = w[:-3]
            elif w == 'dvd版' or w == 'ova版':
              w = w[:-3]
            if w in ydict:
                continue
            if w in self.stopwords:
                continue
            if w == '台':
                if '台湾' in ydict or '台语' in ydict:
                    continue
            if w == '港' and '香港' in ydict:
                continue
            if w == '韩':
                if '韩语' in ydict or '韩国' in ydict:
                    continue
            if w == '日':
                if '日本' in ydict or '日语' in ydict:
                    continue
            if w == '英':
                if '英国' in ydict or '英语' in ydict:
                    continue
            if w == '法':
                if '法国' in ydict or '法语' in ydict:
                    continue
            if w == '德':
                if '德国' in ydict or '德语' in ydict:
                    continue
            if w == 'dvd':
                if '未删减' in ydict: continue
                if '未删减版' in ydict: continue
            if w == '未删减':
                if 'dvd' in ydict: continue
            if w == '外文':
                if '英' in ydict: continue
                if '英文' in ydict: continue
                if '英语' in ydict: continue
            if len(w) >= 6 and w.endswith('语'):
                tmp = w[:-3]+'文'
                if tmp in ydict: continue

            if len(w) >= 6 and w.endswith('文'):
                tmp = w[:-3]+'语'
                if tmp in ydict: continue

            if w.endswith('卫视版'):
                continue
            if w.endswith('卫视') and vt != 4:
                continue
            words.append( w )
        return words

    def compare_part(self, xname, yname):
        xname = xname.replace('国语版', '').replace('国语','').replace('粤语版','').replace('中文版','')
        yname = yname.replace('国语版', '').replace('国语','').replace('粤语版','').replace('中文版','')
        if xname == yname:
            return 0
        xname = xname.lower()
        yname = yname.lower()
        if xname.startswith( yname):
            pass
        elif yname.startswith(xname):
            xname, yname = yname, xname
        else:
            return 0
        diff = xname[len(yname):].strip()
        yname = yname.replace('ova', '')
        if yname not in self.video_series:
            return 0
        if diff in ['1', '第一部', '第一季', '第1季', '第1部', 's1', 'part1', '第1期' ,'第一期', '01']:
            return 0
        if diff.isdigit() and int(diff) < 10:
            return -1
        udiff = diff.decode('utf-8', 'ignore')
        r = re.search(u'第[一二三四五六七八九十0-9]+[部季期]', udiff)
        if not r:
            return 0
        udiff = r.group()
        if udiff:
            w = udiff[1:-1].encode('utf-8', 'ignore')
            if w == '' or w == '01' or w == '一':
                return 0
            return -1
        return 0


    def compare(self, xname, yname, vt ):
        x, y = xname, yname
        if '无字' in x and '有字' in y:
            return -1
        if '无字' in y and '有字' in x:
            return -1
        xori, yori = xname, yname
        xname = xname.decode('utf-8', 'ignore')
        yname = yname.decode('utf-8', 'ignore')
        words1 = self.ext_edition(xname)
        words2 = self.ext_edition(yname)
        d1, d2 = {}, {}
        for w in words1:
            d1[w] = 1
            if w.endswith('版') and len(w) >= 5:
                w = w[:-3]
                d1[w] = 0
        for w in words2:
            d2[w] = 1
            if w.endswith('版') and len(w) >= 5:
                w = w[:-3]
                d2[w] = 0
        xdiffs = self.get_diff_words(d1, d2, vt)
        ydiffs = self.get_diff_words(d2, d1, vt )
        if not xdiffs and not ydiffs:
            if vt != 4 and vt != '4':
                r = self.compare_part(xori, yori)
                return r
            return 0
        diffs = xdiffs + ydiffs
        return -1

gedition = EditionTool()

if __name__ == '__main__':
    edtool = EditionTool()
    f = open('scores.txt', 'rb')
    d = {}
    for line in f:
        line = line.strip()
        if line == '': continue
        fields = line.split("\t")
        if len(fields) != 5: continue
        _, xname, _, yname, sc = fields
        sc = float(sc)
        if xname == yname : continue
        if sc < -0.1: continue
        if sc < 0.6: continue
        vt = 1
        val = edtool.compare(xname, yname, vt)
        if val < 0: continue
        xname = xname.lower()
        yname = yname.lower()
        if xname.find(yname) != -1 or yname.find(xname) != -1:
            continue
        x = xname.decode('utf-8','ignore')
        y = yname.decode('utf-8','ignore')
        x = re.sub('第[0-9一二三四五六七八九十]+[季部]',u'',x)
        y = re.sub('第[0-9一二三四五六七八九十]+[季部]',u'',y)
        x = x.strip()
        y = y.strip()
        if x.find(y) != -1 or y.find(x) != -1:
            continue
        #print u''.join(com)
        print xname, yname
        #break
    f.close()
